import { createStore } from "vuex";
import createPersistedState from 'vuex-persistedstate';
const { context, modules } = loadModules();
const store = createStore({
  modules,
  plugins: [
    createPersistedState({
      storage: window.sessionStorage,
      key: "PersistedState",
      reducer(state) {
        return {
          login: state.login,
        }
      }
    })
  ]
});
module.hot && module.hot.accept(context.id, update);

function loadModules() {
  const context = require.context("@/store/modules", true, /\.js|.ts$/);
  const modulesFiles = context.keys();
  const config = modulesFiles.map((key) => ({ key, name: key.match(/^\.\/(.*)\.\w+$/i)[1] }));
  const modules = config.reduce( (cur, { key, name }) => ({ ...cur, [name]: context(key).default }), {});
  return { context, modules };
}
function update() {
  const { modules } = loadModules();
  store.hotUpdate({ modules });
}
export default store;